package cayte.demo.dict;

import java.io.File;
import java.lang.reflect.Method;

import android.content.Context;
import android.util.Log;

@SuppressWarnings("rawtypes")
public class XfTTS {
	private Class classTTS;
	private Context con;
	private Object objectTTS;

	public XfTTS(Context contex) {
		try {
			con = contex.createPackageContext("com.sinovoice.sharedtts", 3);
			classTTS = Class.forName("com.sinovoice.sharedtts.SharedTTS", true,
					con.getClassLoader());
			Method method = classTTS.getMethod("init");
			method.invoke(null);
			method = classTTS.getMethod("getInstance");
			objectTTS = method.invoke(null);
			method = classTTS.getMethod("setContext", Context.class);
			method.invoke(objectTTS, con);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public int initTTSLib(int param) {
		try {
			Method method = classTTS.getMethod("setContext", Context.class);
			method.invoke(objectTTS, con);
			method = classTTS.getMethod("initTTSLib", Integer.TYPE);
			int j = ((Integer) method.invoke(objectTTS, Integer.valueOf(param)))
					.intValue();
			return j;
		} catch (Exception e) {
			Log.e("hmg", "initTTSLib error ");
			return -1;
		}
	}

	public void playText(String strMsg) {
		try {
			Method method = classTTS.getMethod("playText", String.class);
			method.invoke(objectTTS, strMsg);
			return;
		} catch (Exception e) {
			Log.e("hmg", "playText error ");
		}
	}

	public void playFile(File file, long l) {
		try {
			Class[] cls = new Class[2];
			cls[0] = File.class;
			cls[1] = Long.TYPE;
			Method method = classTTS.getMethod("playFile", cls);
			Object[] obj = new Object[2];
			obj[0] = file;
			obj[1] = Long.valueOf(l);
			method.invoke(objectTTS, obj);
			return;
		} catch (Exception e) {
			Log.e("hmg", "playText error ");
		}
	}

	public int SetParam(int type, long param) {
		try {
			Class[] parmCls = new Class[2];
			parmCls[0] = Integer.TYPE;
			parmCls[1] = Long.TYPE;
			Method method = classTTS.getMethod("ttsSetParam", parmCls);
			Object[] obj = new Object[2];
			obj[0] = Integer.valueOf(type);
			obj[1] = Long.valueOf(param);
			int j = ((Integer) method.invoke(objectTTS, obj)).intValue();
			return j;
		} catch (Exception e) {
			Log.e("hmg", "SetParam error ");
			return -1;
		}
	}

	public int ttsEnd() {
		try {
			Method method = classTTS.getMethod("ttsEnd");
			int j = ((Integer) method.invoke(objectTTS)).intValue();
			return j;
		} catch (Exception e) {
			Log.e("hmg", "ttsEnd error ");
			return -1;
		}
	}

	public void ttsPause() {
		try {
			Method method = classTTS.getMethod("ttsPause");
			method.invoke(objectTTS);
			return;
		} catch (Exception e) {
			Log.e("hmg", "ttsPause");
		}
	}

	public void ttsResume() {
		try {
			Method method = classTTS.getMethod("ttsResume");
			method.invoke(objectTTS);
			return;
		} catch (Exception e) {
			Log.e("hmg", "ttsResume");
		}
	}

	public int ttsStop() {
		try {
			Method method = classTTS.getMethod("ttsStop");
			int j = ((Integer) method.invoke(objectTTS)).intValue();
			return j;
		} catch (Exception e) {
			Log.e("hmg", "ttsStop");
			return -1;
		}
	}
}
